import os
import docx2txt
import re

# 分析示例文档中的分块规则
def analyze_document(doc_path):
    """分析文档的内容，提取由#分隔的块"""
    try:
        # 提取文档文本
        text = docx2txt.process(doc_path)
        # 使用#作为分隔符分块
        chunks = re.split(r'#', text)
        # 移除空块
        chunks = [chunk.strip() for chunk in chunks if chunk.strip()]
        return chunks
    except Exception as e:
        print(f"处理文档 {doc_path} 时出错: {e}")
        return []

def main():
    data_folder = "data"
    docs = []

    # 获取所有docx文件
    for file in os.listdir(data_folder):
        if file.endswith(".docx"):
            docs.append(os.path.join(data_folder, file))
    
    if not docs:
        print("没有找到文档文件")
        return
    
    print(f"找到 {len(docs)} 个文档文件")
    
    # 分析每个文档
    for doc_path in docs:
        print(f"\n分析文档: {os.path.basename(doc_path)}")
        chunks = analyze_document(doc_path)
        
        print(f"共分成 {len(chunks)} 个块")
        # 打印每个块的开头以验证分块结果
        for i, chunk in enumerate(chunks, 1):
            preview = chunk[:100].replace('\n', ' ') + '...' if len(chunk) > 100 else chunk.replace('\n', ' ')
            print(f"块 {i}: {preview}")

if __name__ == "__main__":
    main() 